import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    todo:[
      {
        id:Date.now()+Math.random()*200,
        text:"aaa",
        isDone:false
      },
      {
        id:Date.now()+Math.random()*200,
        text:"vvv",
        isDone:true
      },
      {
        id:Date.now()+Math.random()*200,
        text:"bbb",
        isDone:false
      }
    ]
  },
  getters: {
    count(state){
      return state.todo.filter(item=>item.isDone==false).length;
    }
  },
  mutations: {
    addTodo:(state,{text})=>{
      state.todo = [...state.todo,{
        id:Date.now()+Math.random()*200,
        text,
        isDone:false
      }];
    },
    deleteTodo:(state,{id})=>{
      state.todo = state.todo.filter(item=>item.id!=id);
    },
    changeTodo:(state,{checked,id})=>{
      state.todo = state.todo.map(val=>{
        if(val.id==id)val.isDone=checked;
        return val;
      })
    }
  },
  actions: {
  },
  modules: {
  }
})
